﻿using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using HuaXingSDK;
using HuaXingSDK.Request;
using Nancy;
using Nancy.Helpers;
using Newtonsoft.Json;
using WebTest.Models;
using Formatting = System.Xml.Formatting;

namespace WebTest
{
    public class SDKController : NancyModule
    {
        public SDKController()
        {
            Get["/"] = _ => "华兴SDK测试";
            Post["/kaihu"] = 开户;
            Post["kaihuchaxun"] = 开户查询;
        }

        private string GetParam(string name)
        {
            var gets = (DynamicDictionary) Request.Query;
            if (gets.ContainsKey(name))
                return gets[name].ToString();
            var posts = (DynamicDictionary) Request.Form;
            if (posts.ContainsKey(name))
                return posts[name].ToString();
            var routes = (DynamicDictionary) Context.Parameters;
            if (routes.ContainsKey(name))
                return routes[name].ToString();
            return null;
        }

        private object FromPost(HXRequest request)
        {
            var data = new PostData
            {
                PostUrl = HXConfigs.请求地址,
                RequestDataRaw = request.ToEncryptXml().GetPostMessage(),
                Xml = FmtXml(request.ToXml().GetOuterXml()),
                TransCode = request.TransCode
            };
            data.RequestData = data.RequestDataRaw;
            return View["/Submit.html", data];
        }

        private object ResResult(HXRequest req, HXResponse res)
        {
            return View["/Res.html", new
            {
                res = HttpUtility.HtmlEncode(JsonConvert.SerializeObject(res)),
                req = HttpUtility.HtmlEncode(req.ToXml().GetOuterXml())
            }];
        }

        private object 开户(object _)
        {
            var req = new Req账户开立
            {
                ACName = GetParam("name"),
                IDNo = GetParam("acno"),
                Mobile = GetParam("mobile"),
                Email = GetParam("email"),
                ReturnUrl = "123"
            };
            if (req.ACName != null)
            {
                req.IDType = HXIDType.居民身份证;
            }
            return FromPost(req);
        }

        private object 开户查询(object _)
        {
            var seqno = GetParam("seqno");
            var req = new Req账户开立查询(seqno);
            var res = req.ToResponse();
            return ResResult(req, res);
        }

        private static Regex _regex = new Regex(">\\s+</");

        private static string FmtXml(string xmlstr)
        {
            var xml = new XmlDocument();
            xml.LoadXml(xmlstr);
            var sb = new StringBuilder();
            using (var sw = new StringWriter(sb))
            {
                using (var xtw = new XmlTextWriter(sw))
                {
                    xtw.Indentation = 1;
                    xtw.Formatting = Formatting.Indented;
                    xtw.IndentChar = '\t';
                    xml.WriteTo(xtw);
                }
            }
            return _regex.Replace(sb.ToString(), "></");
        }
    }
}